package com.nhxcz.bigdataweb.controller;


import com.nhxcz.bigdataweb.entity.Count;
import com.nhxcz.bigdataweb.service.serviceImpl.CountService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//坐标转换需要
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.StringTokenizer;

import java.util.ArrayList;

@Controller
@RequestMapping("/count")
public class countController {
    @Autowired
    private CountService countService;
    public ArrayList<Count> oldCountList = new ArrayList<Count>();

    @RequestMapping("/getCount")
    public Count getUser(@Param("longitude") double longitude) {
        Count coun = countService.getLonLan(longitude);
        return coun;
    }

    @RequestMapping("/index")
    public String index() {
        return "index";
    }

    @ResponseBody
    @RequestMapping("/print")
    public ArrayList<Count> print() {
        ArrayList<Count> countList = countService.print();
//        for (Count count : countList) {
//            String lonlat = turn(count.getLongitude(), count.getLatitude());
//            StringTokenizer st = new StringTokenizer(lonlat, ",");
//            count.setLongitude((Double.parseDouble(st.nextToken())));
//            count.setLatitude(Double.parseDouble(st.nextToken()));
//        }
        return countList;
    }

    @RequestMapping("/heatmap")
    public String heatmap() {
        return "heatmap";
    }

    @RequestMapping("/unugather")
    public String unugather() {
        return "gather";
    }

    @ResponseBody
    @RequestMapping("/top")
    public ArrayList<Count> top() {
        ArrayList<Count> countLi = countService.top();
//        for (int i = 0; i < countLi.size(); i++) {
//            Count count = countLi.get(i);
//            String lonandlat = turn(count.getLongitude(), count.getLatitude());
//            StringTokenizer st = new StringTokenizer(lonandlat, ",");
//            count.setLongitude((Double.parseDouble(st.nextToken())));
//            count.setLatitude(Double.parseDouble(st.nextToken()));
//        }
        return countLi;
    }

    @ResponseBody
    @RequestMapping("/bus")
    public int bus(){
           int bus = countService.bus();
           return bus;
    }

    @ResponseBody
    @RequestMapping("/car")
    public int car(){
        int car = countService.car();
        return car;
    }

    @ResponseBody
    @RequestMapping("/walk")
    public int walk(){
        int walk = countService.walk();
        return walk;
    }

    @ResponseBody
    @RequestMapping("/bike")
    public int bike(){
        int bike = countService.bike();
        return bike;
    }

    @ResponseBody
    @RequestMapping("/train")
    public int train(){
        int train = countService.train();
        return train;
    }

    @ResponseBody
    @RequestMapping("/traffic")
    public int traffic(){
        int traffic = countService.traffic();
        return traffic;
    }
    
    @ResponseBody
    @RequestMapping("/time")
    public String time(){
        String t= countService.time();
        System.out.println(t);
        return t;
    }

//    public static String connectURL(String dest_url, String commString) {
//        String rec_string = "";
//        URL url = null;
//        HttpURLConnection urlconn = null;
//        OutputStream out = null;
//        BufferedReader rd = null;
//        try {
//            url = new URL(dest_url);
//            urlconn = (HttpURLConnection) url.openConnection();
//            urlconn.setReadTimeout(1000 * 30);
//            urlconn.setRequestMethod("POST");
//            urlconn.setDoInput(true);
//            urlconn.setDoOutput(true);
//            out = urlconn.getOutputStream();
//            out.write(commString.getBytes("UTF-8"));
//            out.flush();
//            out.close();
//            rd = new BufferedReader(new InputStreamReader(urlconn.getInputStream()));
//            StringBuffer sb = new StringBuffer();
//            int ch;
//            while ((ch = rd.read()) > -1)
//                sb.append((char) ch);
//            rec_string = sb.toString();
//        } catch (Exception e) {
//            return "";
//        } finally {
//            try {
//                if (out != null) {
//                    out.close();
//                }
//                if (urlconn != null) {
//                    urlconn.disconnect();
//                }
//                if (rd != null) {
//                    rd.close();
//                }
//            } catch (Exception e) {
//            }
//        }
//        return rec_string;
//    }

//    public String turn(double longitude,double latitude) {
//        StringBuffer lonandlat = new StringBuffer();
//        int i=0;
//        String string="";
//        String coords = longitude+","+latitude;
//        String result =connectURL("http://api.map.baidu.com/geoconv/v1/?coords="+coords+"&from=1&to=5&output=json&ak=k8qty39Yu17dSx7WFqejVIO1snGRGtFf","");
////        System.out.println(result);
//        StringTokenizer  st=new StringTokenizer(result,":");
//        while(st.hasMoreElements()) {
//            string=st.nextToken();
////            System.out.println(string);
//            if(i==3) {
//                lonandlat.append(string);
//                lonandlat.delete(12,string.length());
//                System.out.println(lonandlat.toString());
//            }
//            else if(i==4) {
//                lonandlat.append(",");
//                lonandlat.append(string);
//                lonandlat.delete(24,string.length()+13);
//                System.out.println(lonandlat.toString());
//            }
//            i++;
//        }
//        string=lonandlat.toString();
//        return string;
//    }
}
